//
//  HNSH_RefundApplyListViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/3/7.
//

#import "HNSH_RefundApplyListViewController.h"

//view
#import "HNSH_OrderListCell.h"
#import "HNSH_OrderListHeader.h"
#import "HNSH_RefundApplyListFooter.h"

#import "HNSH_RefundDetailViewController.h"

@interface HNSH_RefundApplyListViewController ()

/* listAry */
@property (nonatomic, strong) NSMutableArray *listAry;

@end

static NSString *HNSH_OrderListCellID = @"HNSH_OrderListCellID";
static NSString *HNSH_OrderListHeaderID = @"HNSH_OrderListHeaderID";
static NSString *HNSH_RefundApplyListFooterID = @"HNSH_RefundApplyListFooterID";

@implementation HNSH_RefundApplyListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"售后/退款" showLeftItem:YES];
    
    [self configTableView];
    
}

- (void)configTableView {
    [self setupEmpty:self.tableView image:[UIImage imageNamed:@"empty_list_data"] title:@""];
    [self setupRefresh:self.tableView option:ST_RefreshDefault];
    
    self.tableView.rowHeight = 106;
    [self.tableView registerClass:[HNSH_OrderListCell class] forCellReuseIdentifier:HNSH_OrderListCellID];
    [self.tableView registerClass:[HNSH_OrderListHeader class] forHeaderFooterViewReuseIdentifier:HNSH_OrderListHeaderID];
    [self.tableView registerClass:[HNSH_RefundApplyListFooter class] forHeaderFooterViewReuseIdentifier:HNSH_RefundApplyListFooterID];
}

- (void)refreshData:(NSInteger)page {
    [HNSH_ShopNetServer requestGoodsOrderAfterSalesListWithPageNum:page success:^(ST_BaseNetModel * _Nullable responseObject) {
        [self endRefresh:YES];
        if (responseObject.code == 0) {
            if (page == 1) {
                [self.listAry removeAllObjects];
            }
            NSArray * records = responseObject.resultset[@"list"];
            NSArray * array = [NSArray yy_modelArrayWithClass:HNSH_OrderListModel.class json:records];
            [self.listAry addObjectsFromArray:array];
            [self.tableView reloadData];
            [self endRefresh:array.count>=10];
        }
        
    } Failure:^(NSError * _Nullable error) {
        
    }];
}


//MARK: 点击按钮
- (void)clickBtnWithBtn:(UIButton *)sender model:(HNSH_OrderListModel *)model {
    if ([sender.currentTitle isEqualToString:@"查看详情"]) {
        [self pushDetailVCWithModel:model];
    }
    if ([sender.currentTitle isEqualToString:@"删除记录"]) {
        [self showViewWithText:@"确认删除这条售后信息吗？" model:model];
    }
}

- (void)pushDetailVCWithModel:(HNSH_OrderListModel *)model{
    HNSH_RefundDetailViewController * vc = [HNSH_RefundDetailViewController new];
    vc.orderId = model.order_id;
    [self.navigationController pushViewController:vc animated:YES];
}

//MARK: 展示弹框
- (void)showViewWithText:(NSString *)text model:(HNSH_OrderListModel *)model{
    WeakSelf(weakSelf);
    [HNSH_AlertView showTitle:text des:@"" buttonTitle:@"确定" buttonColor:[UIColor appGreenColor] buttonClick:^{
        [weakSelf deleteOrderWithModel:model];
    }];

}

- (void)deleteOrderWithModel:(HNSH_OrderListModel *)model{
    [HNSH_ShopNetServer requestGoodsOrderAfterSalesDeleteWithOrderId:model.uid success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [self headerRefreshing];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark lazy
- (NSMutableArray *)listAry {
    if (!_listAry) {
        _listAry = [NSMutableArray arrayWithCapacity:0];
    }
    return _listAry;
}

#pragma mark - delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listAry.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    HNSH_OrderListModel *model = self.listAry[section];
    return model.items.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 88.01f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 68.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    HNSH_RefundApplyListFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_RefundApplyListFooterID];
    HNSH_OrderListModel *model = self.listAry[section];
    footer.model = model;
    WeakSelf(weakSelf);
    footer.buttonBlock = ^(UIButton * _Nonnull button, HNSH_OrderListModel * _Nonnull itemModel) {
        [weakSelf clickBtnWithBtn:button model:itemModel];
    };
    return footer;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    HNSH_OrderListHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:HNSH_OrderListHeaderID];
    HNSH_OrderListModel *model = self.listAry[section];
    header.model = model;
    return header;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_OrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:HNSH_OrderListCellID];
    HNSH_OrderListModel *model = self.listAry[indexPath.section];
    cell.model = model.items[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HNSH_OrderListModel *model = self.listAry[indexPath.section];
    HNSH_RefundDetailViewController *vc = [[HNSH_RefundDetailViewController alloc]init];
    vc.orderId = model.order_id;
    [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
